package com.summer.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.mapper.OrderRenewalMapper;
import com.summer.util.BuildXLSX;
import com.summer.util.CallBackResult;
import com.summer.util.XlsxParam;
import com.summer.vo.RenewalUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Desc:
 * Created by tl on 2018/12/21
 */
@Controller
@RequestMapping("/v1.0/api/renewalOrder")
public class OrderRenewalController {
    private static Logger log = LoggerFactory.getLogger(OrderRenewalController.class);
    @Resource
    private OrderRenewalMapper orderRenewalMapper;

    /**
     * 展期记录列表
     *
     * @param jsonData
     * @return
     */
    @ResponseBody
    @PostMapping("/queryWithUser")
    public String queryWithUser(@RequestBody String jsonData) {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("OrderRenewalController queryWithUser params=" + params.toString());
        PageHelper.startPage(params);
        List<RenewalUserVO> list = orderRenewalMapper.findParams(params);
        return CallBackResult.returnJson(new PageInfo<>(list));
    }

    /**
     * 导出展期记录列表
     *
     * @param request
     * @param response
     * @param params
     * @throws Exception
     */
    @RequestMapping("downloadOrderRenewal")
    public void downloadOrderRenewal(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam Map<String, Object> params) throws Exception {
        log.info("OrderRenewalController downloadOrderRenewal params=" + params.toString());
        List<RenewalUserVO> userVOList = orderRenewalMapper.findParams(params);
        log.info("OrderRenewalController downloadOrderRenewal size=" + userVOList.size());
        for (RenewalUserVO renewalUserVO : userVOList) {
            renewalUserVO.setAmountToPay(renewalUserVO.getRepaymentAmount() - renewalUserVO.getPaidAmount());
        }
        String title = "展期订单列表";
        BuildXLSX.setFileDownloadHeader(request, response, title);
        LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
        titleMap.put("createTimeChg", "展期时间");
        titleMap.put("realName", "客户姓名");
        titleMap.put("phone", "手机号码");
        titleMap.put("amountToPay", "应还金额");
        titleMap.put("renewalFee", "展期费");

        titleMap.put("renewalDay", "展期期限");
        titleMap.put("repaymentTimeChg", "应还款时间");

        titleMap.put("status", "展期状态");
        titleMap.put("remark", "失败原因");
        titleMap.put("payType", "支付方式");
        titleMap.put("renewalNo", "展期次数");
        titleMap.put("outTradeNo", "交易号");
        titleMap.put("reviewer", "审核人");
        XlsxParam xlsxParam = new XlsxParam(userVOList, title, titleMap);
        OutputStream os = response.getOutputStream();
        BuildXLSX.buildExcel(xlsxParam, os);
    }
}
